using System;
using System.Collections;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class UIHUD : UIBase
{
    public Button leftButton;
    public Button rightButton;
    public Text leftText;
    public Text rightText;
    public Image bgButton;
    public Color normal;
    public Color highlight;
    
    public Button restartButton;

    public CanvasGroup downButtonCG;
    public CanvasGroup restartButtonCG;
    

    private int mineNum;

    public int MineNum
    {
        get{return mineNum;}
        set
        {
            mineNum=value;
            minesText.text=mineNum.ToString();
        }
    }
    public Text minesText;
    public Button backButton;
    

    private void Start()
    {
        UIEvent.Instance.onUIHUDClickLeftButton += OnClickLeftButton;
        UIEvent.Instance.onUIHUDClickRightButton += OnClickRightButton;
        UIEvent.Instance.onUIHUDClickBackButton += OnClickBackButton;
        UIEvent.Instance.onUIHUDClickReStartButton += OnClickRestartButton;
        
        
        leftButton.onClick.AddListener(UIEvent.Instance.OnHUDClickLeftButton);
        rightButton.onClick.AddListener(UIEvent.Instance.OnHUDClickRightButton);
        backButton.onClick.AddListener(UIEvent.Instance.OnHUDClickBackButton);
        
        restartButton.onClick.AddListener(UIEvent.Instance.OnUIHUDClickReStartButton);
    }

    private void OnClickRestartButton()
    {
        Debug.LogFormat("Restarting level {0}", LevelManager.Instance.currentLevelIndex);
        GameManager.Instance.gameController.OnClickBackButton();
        UIEvent.Instance.OnUIGameStartClickNewGame();
    }

    public override void OnEnter()
    {
        base.OnEnter();
        
        if (GameManager.Instance.gameController.firstClick)
        {
            HideDownButton();
            HideRestartButton();
        }
        else
        {
            ShowDownButton();
            if (GameManager.Instance.gameController.isLeft)
            {
                OnClickLeftButton();
            }
            else
            {
                OnClickRightButton();
            }
        }
    }

    public void OnClickLeftButton()
    {
        StopCoroutine("SelectRight");
        GameManager.Instance.gameController.isLeft = true;
        StartCoroutine(SelectLeft(.5f));
    }

    private void OnClickRightButton()
    {
        StopCoroutine("SelectLeft");
        GameManager.Instance.gameController.isLeft = false;
        StartCoroutine(SelectRight(.5f));
    }

    private void OnClickBackButton()
    {
        Debug.LogFormat("Clicking back button");
        UIManager.Instance.CloseUI(this);
    }
    
    
    
    
    public IEnumerator SelectLeft(float duration)
    {
        float elapsed = 0;
        while (elapsed < duration)
        {
            elapsed += Time.deltaTime;
            leftText.color = ExtenSionMethod.ColorLerp(leftText.color,normal,duration);
            leftButton.GetComponent<Image>().color = ExtenSionMethod.ColorLerp(leftButton.GetComponent<Image>().color,highlight,duration);
            rightText.color = ExtenSionMethod.ColorLerp(rightText.color,highlight,duration);
            rightButton.GetComponent<Image>().color = ExtenSionMethod.ColorLerp(rightButton.GetComponent<Image>().color,normal,duration);
            //bgButton.color = ExtenSionMethod.ColorLerp(bgButton.color,normal,duration);
            yield return null;
        }
    }
    
    public IEnumerator SelectRight(float duration)
    {
        float elapsed = 0;
        while (elapsed < duration)
        {
            elapsed += Time.deltaTime;
            rightText.color = ExtenSionMethod.ColorLerp(rightText.color,normal,duration);
            rightButton.GetComponent<Image>().color = ExtenSionMethod.ColorLerp(rightButton.GetComponent<Image>().color,highlight,duration);
            leftText.color = ExtenSionMethod.ColorLerp(leftText.color,highlight,duration);
            leftButton.GetComponent<Image>().color = ExtenSionMethod.ColorLerp(leftButton.GetComponent<Image>().color,normal,duration);
            //bgButton.color = ExtenSionMethod.ColorLerp(bgButton.color,highlight,duration);
            yield return null;
        }
    }

    public void HideDownButton()
    {
        downButtonCG.DOKill();
        downButtonCG.alpha = 0;
        downButtonCG.blocksRaycasts = false;
    }

    public void ShowDownButton()
    {
        Debug.LogFormat("UIHUD Showing down button");
        HideRestartButton();
        downButtonCG.DOKill();
        downButtonCG.blocksRaycasts = true;
        DOTween.To(()=>downButtonCG.alpha, x => downButtonCG.alpha = x, 1, 1f);
    }

    public void HideRestartButton()
    {
        restartButtonCG.DOKill();
        restartButtonCG.alpha = 0;
        restartButtonCG.blocksRaycasts = false;
    }

    public void ShowRestartButton()
    {
        Debug.LogFormat("UIHUD Showing Restart button");
        HideDownButton();
        restartButtonCG.DOKill();
        restartButtonCG.blocksRaycasts = true;
        DOTween.To(()=>restartButtonCG.alpha, x => restartButtonCG.alpha = x, 1, 1f);
    }
    
}
